package com.gjs.freechat.web.controller;

import java.nio.ByteBuffer;

import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

import com.alibaba.fastjson.JSON;
import com.gjs.freechat.server.component.bean.Message;
import com.gjs.freechat.utils.MyStringUtils;
import com.gjs.freechat.web.business.WebUserConnectionPool;

@ServerEndpoint("/ws/chat/{userId}")
public class UserConnectionWebSocket {
	
	public static WebUserConnectionPool connPool;
	
	@OnOpen
	public void onOpen(Session session, @PathParam("userId") String userId) {
		session.setMaxIdleTimeout(1000*60*60);
		connPool.addNewUserConnection(session.getId(), userId, session);
	}
	
	@OnClose
	public void onClose(CloseReason closeReason, Session session, @PathParam("userId") String userId) {
		connPool.removeConnection(closeReason, session, userId);
	}
	
	@OnMessage
	public void onMessage(String textMessage, @PathParam("userId") String userId) {
		connPool.onMessage(textMessage, userId);
	}
	
	@OnMessage
	public void onMessage(ByteBuffer byteBuffer) {
	}
	
	@OnError
	public void onError(Throwable throwable) {
		
	}
}
